﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastIOC.Annotation;
using FastORM.Applicaiton;
using FastUtil.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastExecutor.ORG.DB
{
    [Component]
    public class DB_Attach
    {
        public string StorageType = System.Configuration.ConfigurationManager.AppSettings["AttachStorageType"];
        public string VirtualPath = System.Configuration.ConfigurationManager.AppSettings["AttachVirtualPath"];
        [AutoWired]
        public DBUtil @DBUtil;
        [AutoWired]
        private DB_AttachConfig @DB_AttachConfig;

        public void Insert(Frame_AttachInfo AttachInfo, byte[] FileContent)
        {

            string UploadVirtualPath = System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualPath);
            DirectoryInfo UploadPath = new DirectoryInfo(UploadVirtualPath);
            if (!UploadPath.Exists)
            {
                UploadPath.Create();
            }
            AttachInfo.RowGuid = CommonUtil.CreateCommonGuid();
            AttachInfo.AddDate = DateTime.Now;
            AttachInfo.AddUserGuid = "sysadmin";
            AttachInfo.AddUserName = "系统管理员";
            AttachInfo.StorageType = StorageType;
            if (StorageType == "Local")
            {
                AttachInfo.ConfigName = "Local";
                AttachInfo.StorageGuid = string.Empty;
                string UploadFileFloderPath = VirtualPath + "/" + AttachInfo.RowGuid;
                DirectoryInfo UploadFileFloder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/" + UploadFileFloderPath));
                if (!UploadFileFloder.Exists)
                {
                    UploadFileFloder.Create();
                }
                string UploadFileVirtualPath = UploadFileFloderPath + "/" + AttachInfo.FileName;
                AttachInfo.VirtualPath = UploadFileVirtualPath;
                string UploadFileSavePath = System.Web.HttpContext.Current.Server.MapPath("~/" + UploadFileVirtualPath);
                FileUtil.SaveFile(UploadFileSavePath, FileContent);
                @DBUtil.Insert(AttachInfo);
            }
            else if (StorageType == "DB")
            {
                AttachInfo.ConfigName = GetAttachConfigName();
                string AttachStorageGuid = CommonUtil.CreateCommonGuid();
                AttachInfo.StorageGuid = AttachStorageGuid;
                AttachInfo.VirtualPath = string.Empty;
                @DBUtil.Insert(AttachInfo);
                Frame_AttachStorage AttachStorage = new Frame_AttachStorage();
                AttachStorage.RowGuid = AttachStorageGuid;
                AttachStorage.FileName = AttachInfo.FileName;
                AttachStorage.FileSize = AttachInfo.FileSize;
                AttachStorage.GroupGuid = AttachInfo.GroupGuid;
                AttachStorage.GroupType = AttachInfo.GroupType;
                AttachStorage.AddDate = DateTime.Now;
                AttachStorage.Content = FileContent;
                @DBUtil.Insert(AttachStorage, AttachInfo.ConfigName);
            }
        }

        public bool ExistAttach(string GroupGuid, string GroupType,out string AttachGuid)
        {
            AttachGuid = string.Empty;
            string sql = string.Format("select * from Frame_AttachInfo where GroupGuid='{0}' and GroupType='{1}' ", GroupGuid, GroupType);
            List<Frame_AttachInfo> AttachInfoList = @DBUtil.FindList<Frame_AttachInfo>(sql);
            if (AttachInfoList.Count>0)
            {
                AttachGuid = AttachInfoList[0].RowGuid;
                return true;
            }
            else
            {
                return false;
            }
        }

        public List<Frame_AttachInfo> GetAttachList(string GroupGuid, string GroupType = "")
        {
            string sql = string.Format("select * from Frame_AttachInfo where GroupGuid='{0}' ", GroupGuid);
            if (!string.IsNullOrEmpty(GroupType))
            {
                sql += string.Format(" and GroupType='{0}'", GroupType);
            }
            List<Frame_AttachInfo> AttachInfoList = @DBUtil.FindList<Frame_AttachInfo>(sql);
            return AttachInfoList;

        }

        public void DeleteAttach(string AttachGuid)
        {
            Frame_AttachInfo AttachInfo = @DBUtil.FindOne<Frame_AttachInfo>(AttachGuid);
            if (AttachInfo.StorageType == "Local")
            {
                string FilePath = System.Web.HttpContext.Current.Server.MapPath("~/" + AttachInfo.VirtualPath);
                File.Delete(FilePath);
                DirectoryInfo UploadFileFloder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/" + AttachInfo.VirtualPath.Replace("/" + AttachInfo.FileName, "")));
                if (UploadFileFloder.Exists)
                {
                    UploadFileFloder.Delete();
                }

            }
            else if (AttachInfo.StorageType == "DB")
            {
                @DBUtil.ExecuteSQL(string.Format("delete from Frame_AttachStorage where RowGuid='{0}'", AttachInfo.StorageGuid), AttachInfo.ConfigName);
            }
            @DBUtil.Remove(AttachInfo);
        }

        public void DeleteGroupAttach(string GroupGuid, string GroupType = "")
        {
            string sql = string.Format("select * from Frame_AttachInfo where GroupGuid='{0}' ", GroupGuid);
            if (!string.IsNullOrEmpty(GroupType))
            {
                sql += string.Format(" and GroupType='{0}'", GroupType);
            }
            List<Frame_AttachInfo> AttachInfoList = @DBUtil.FindList<Frame_AttachInfo>(sql);
            foreach (Frame_AttachInfo AttachInfo in AttachInfoList)
            {
                if (AttachInfo.StorageType == "Local")
                {
                    string FilePath = System.Web.HttpContext.Current.Server.MapPath("~/" + AttachInfo.VirtualPath);
                    File.Delete(FilePath);
                    DirectoryInfo UploadFileFloder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/" + AttachInfo.VirtualPath.Replace("/" + AttachInfo.FileName, "")));
                    if (UploadFileFloder.Exists)
                    {
                        UploadFileFloder.Delete();
                    }

                }
                else if (AttachInfo.StorageType == "DB")
                {
                    @DBUtil.ExecuteSQL(string.Format("delete from Frame_AttachStorage where RowGuid='{0}'", AttachInfo.StorageGuid), AttachInfo.ConfigName);
                }
                @DBUtil.Remove(AttachInfo);
            }
        }

        private string GetAttachConfigName()
        {
            string AttachConfigName = "Base_ConnectionString";
            List<Frame_AttachConfig> ConfigList = @DB_AttachConfig.FindAll(" and IsEnable=1");
            if (ConfigList.Count > 0)
            {
                AttachConfigName = ConfigList[0].ConfigName;
            }
            return AttachConfigName;
        }
    }
}
